package com.topscomm.gpm.script;

import com.topscomm.basic.BasicEntity;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.service.ICboApprovalNodeInsService;
import com.topscomm.cbo.service.ICboUserService;
import com.topscomm.cbo.service.impl.CboApprovalNodeInsService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.gpm.GpmScript;
import com.topscomm.gpm.GpmSystemConst;
import com.topscomm.gpm.pojo.*;
import com.topscomm.gpm.service.*;
import com.topscomm.gpm.service.impl.*;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wudaliang
 * @date 2021/1/11
 * @description:
 * @modify:
 */
public class GpmProjectPlanScript extends GpmScript {



    /**
     * @author: wudaliang
     * @date: 2021-01-19
     * @description:
     * @return: void
     * @modify:
     */
    @Transactional
    public void projectPlanAfterApproval(ITapService service, TapEntity entity)throws Exception{
        //审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        IGpmProjectTaskService gpmProjectTaskService = (GpmProjectTaskService) SpringUtil.getBean("gpmProjectTaskService");
        IGpmProjectPlanService gpmProjectPlanService = (GpmProjectPlanService) SpringUtil.getBean("gpmProjectPlanService");
        IGpmProjectMilepostService gpmProjectMilepostService = (GpmProjectMilepostService) SpringUtil.getBean("gpmProjectMilepostService");
        IGpmProjectService gpmProjectService = (GpmProjectService) SpringUtil.getBean("gpmProjectService");
        ICboApprovalNodeInsService cboApprovalNodeInsService = (CboApprovalNodeInsService) SpringUtil.getBean("cboApprovalNodeInsService");
        ICboUserService cboUserService = (CboUserService) SpringUtil.getBean("cboUserService");
        IGpmProjectTaskFeedbackService gpmProjectTaskFeedbackService = (GpmProjectTaskFeedbackService) SpringUtil.getBean("gpmProjectTaskFeedbackService");
        IGpmProjectTaskCloseService gpmProjectTaskCloseService = (GpmProjectTaskCloseService) SpringUtil.getBean("gpmProjectTaskCloseService");
        StringBufferProxy sql = new StringBufferProxy();
        // 查询新策划书
        GpmProjectPlanEntity sdNewPlan = gpmProjectPlanService.queryById(sourceid);
        //查询是否有生效的策划书
        sql.appendLineSingle("state=9 and enabled=1 and projectcode= '{0}'",sdNewPlan.getProjectcode());
        List<GpmProjectPlanEntity> sdProjectPlanEntities = gpmProjectPlanService
            .queryByWhere(sql.toString());
        if(sdProjectPlanEntities.size()>0) {
            // 查询旧策划书
            GpmProjectPlanEntity sdOldPlan = sdProjectPlanEntities.get(0);
            // 查询旧策划书的里程碑
            sql.clear();
            sql.appendLineSingle("planid = '{0}'",sdOldPlan.getId());
            List<GpmProjectMilepostEntity> sdOldMilepostList = gpmProjectMilepostService
                    .queryByWhere(sql.toString());
            // 查询新策划书的里程碑
            sql.clear();
            sql.appendLineSingle("planid = '{0}'",sdNewPlan.getId());
            List<GpmProjectMilepostEntity> sdNewMilepostList = gpmProjectMilepostService
                    .queryByWhere(sql.toString());
            // 将旧策划书有效性置为0，并把旧策划书下的里程碑有效性全部置为0,
            // 将新策划书有效性置为1，并把新策划书下里程碑有效性全部置为1，同步新里程碑和旧里程碑的状态
            gpmProjectPlanService.disableOldPlanAndMilePost(sdOldPlan,sdOldMilepostList,sdNewPlan,sdNewMilepostList);
            // 修改任务中的里程碑名称
            gpmProjectPlanService.updateTaskMilepostName(sdNewPlan,sdNewMilepostList);
            // 如果新里程碑改变了里程碑责任人，需要将任务反馈和任务关闭的里程碑责任人节点的办理人进行修改
            gpmProjectPlanService.updateResponsibleCode(sdOldMilepostList,sdNewMilepostList);
        } else {
            sql.clear();
            sql.appendSingle("planid = '{0}'",sdNewPlan.getId());
            // 获取该策划书下的里程碑
            List<GpmProjectMilepostEntity> gpmProjectMilepostEntityList = gpmProjectMilepostService
                .queryByWhere(sql.toString());
            List<BasicEntity> updateMilepostEntity = new ArrayList<>();
            // 将各个里程碑的状态置为1,并更新任务的里程碑名称字段
            for (GpmProjectMilepostEntity gpmProjectMilepostEntity : gpmProjectMilepostEntityList) {
                gpmProjectMilepostEntity.setEnabled(1);
                updateMilepostEntity.add(gpmProjectMilepostEntity);
                gpmProjectPlanService.updateMilepostNameForTask(gpmProjectMilepostEntity);
            }
            gpmProjectMilepostService.updateBatch(updateMilepostEntity);
        }


        //查询项目entity
        sql.clear();
        sql.appendLineSingle("projectcode = '{0}'", sdNewPlan.getProjectcode());
        List<GpmProjectEntity> sdProjectEntities = gpmProjectService.queryByWhere(sql.toString());
        if(!CollectionUtils.isEmpty(sdProjectEntities)){
            GpmProjectEntity gpmProjectEntity = sdProjectEntities.get(0);
            //策划审批结束，更细项目状态。更新状态：执行阶段（策划审批结束到里程碑全部完成）
            gpmProjectEntity.setProjectstate(GpmSystemConst.SdProjectState.ProjectExecution);
            gpmProjectService.updateEntity(gpmProjectEntity);
        }
        // 如果新里程碑已全部完成，则更新项目状态为复盘阶段
        if(!CollectionUtils.isEmpty(sdProjectEntities)) {
            gpmProjectTaskService.queryMilepostAllFinished(sdProjectEntities.get(0));
        }
    }

    /**
     * @author
     * @date
     * @description: 策划变更结束，给项目组成员发送消息
     * @modify:
     */
    @Transactional
    public void projectPlanSendMessage(ITapService service, TapEntity entity) throws Exception {
        // 审批点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        String sourcetype = ConvertUtil.convertToString(nodeins.getSourcetype());
        GpmProjectPlanService gpmProjectPlanService = (GpmProjectPlanService) SpringUtil.getBean("gpmProjectPlanService");
        GpmProjectTeamService gpmProjectTeamService = (GpmProjectTeamService) SpringUtil.getBean("gpmProjectTeamService");
        GpmProjectTaskService gpmProjectTaskService = (GpmProjectTaskService) SpringUtil.getBean("gpmProjectTaskService");
        GpmProjectMilepostService gpmProjectMilepostService = (GpmProjectMilepostService) SpringUtil.getBean("gpmProjectMilepostService");
        // 查询项目编号
        StringBufferProxy sql = new StringBufferProxy();
        GpmProjectPlanEntity gpmProjectPlanEntity = gpmProjectPlanService.queryById(sourceid);
        sql.appendSingle("projectcode = '{0}'", gpmProjectPlanEntity.getProjectcode());
        // 查询项目是否为第一次策划
        int count = gpmProjectPlanService.getCount(sql.toString());
        if (count > 1) {
            // 消息内容
            String contentInfo = "策划书";
            // 给项目组成员发送消息
            gpmProjectTeamService.sendMessageToGroupMember(sql.toString(), sourceid, sourcetype, contentInfo);
        }
        // 批量下发任务并通知任务责任人任务已下发
        String milepostCodes = gpmProjectTaskService.sendMessageToTaskResponsible(gpmProjectPlanEntity.getProjectcode());
        // 通知里程碑责任人此里程碑下所有任务已下发
        gpmProjectMilepostService.sendMessageToMilepostResponsible(milepostCodes);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveBaseChangeLog(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        GpmProjectPlanService gpmProjectPlanService = (GpmProjectPlanService) SpringUtil.getBean("gpmProjectPlanService");
        // 获取任命书信息
        GpmProjectPlanEntity gpmProjectPlanEntity = gpmProjectPlanService.queryById(sourceid);
        if (gpmProjectPlanEntity.getVersion() > 1) {
            gpmProjectPlanService.changeEditNodePass(sourceid);
        }
    }

}
